package com.glxy.dao.impl;

import com.glxy.dao.ProviderDao;
import com.glxy.pojo.Address;
import com.glxy.pojo.Provider;
import com.glxy.pojo.User;
import com.glxy.utils.DBUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ProviderDaoImpl implements ProviderDao {

    private static final String table = "smbms_provider";

    DBUtils dbUtils = DBUtils.getInstance();



    public ArrayList<Provider> findByName(String proName) {
        String sql = "select * from "+table+" where proName = '"+proName+"';";
        ArrayList<Provider> providerList = dbUtils.executeQuery(sql,null,Provider.class);
        if( !providerList.isEmpty()){
            return  providerList;
        }else {
            return  null;
        }
    }

    @Override
    public ArrayList<Provider> findAll() {
        String sql = "select * from "+table+";";
        return dbUtils.executeQuery(sql, null, Provider.class);
    }

    @Override
    public boolean insert(Provider provider) {
        StringBuffer sql = new StringBuffer("insert into "+table+"(");
        StringBuffer placeholder = new StringBuffer();
        ArrayList<Object> params = new ArrayList<>();

        try {
            Iterator<Field> it = Arrays.stream(Provider.class.getDeclaredFields()).iterator();
            Field field = it.next();

            field.setAccessible(true);
            if (field.get(provider) != null) {
                sql.append(field.getName());
                placeholder.append("?");
                params.add(field.get(provider));
            }
            while (it.hasNext()) {
                field = it.next();
                field.setAccessible(true);
                if (field.get(provider) != null) {
                    sql.append(", ").append(field.getName());
                    placeholder.append(", ").append("?");
                    params.add(field.get(provider));
                }

            }
            sql.append(") values (").append(placeholder).append(");");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return dbUtils.execute(new String(sql), params) == 1;
    }

    @Override
    public Integer update(Provider provider) {
        StringBuffer sql = new StringBuffer("update "+table+" set ");
        ArrayList<Object> params = new ArrayList<>();

        Iterator<Field> it = Arrays.stream(Address.class.getDeclaredFields()).iterator();


        try {
            Field field = it.next();
            field.setAccessible(true);
            Long id = (Long) field.get(provider);

            field = it.next();
            field.setAccessible(true);
            if (field.get(provider) != null) {
                sql.append(field.getName()).append("=?");
                params.add(field.get(provider));
            }
            while (it.hasNext()) {
                field = it.next();
                field.setAccessible(true);
                if (field.get(provider) != null) {
                    sql.append(", ").append(field.getName()).append("=?");
                    params.add(field.get(provider));
                }
            }
            sql.append(" where id = ").append(id).append(";");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }



        return dbUtils.execute(new String(sql), params);
    }

    @Override
    public boolean delete(Long id) {
        StringBuffer sql = new StringBuffer("delete from "+table+" where id = "+id+";");

        return dbUtils.execute(new String(sql), null) == 1;
    }
}
